-
Notifications
You must be signed in to change notification settings - Fork 67
Add wp db status command for database health overview
#307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
wp db status command for database health overview
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a new wp db status command, which provides a convenient overview of the database's health and configuration. The implementation is solid and consolidates information from several other commands. My review focuses on improving the new Behat tests for better robustness and refactoring parts of the new status() method to enhance code clarity, reduce duplication, and improve maintainability.
| Scenario: Display database status for a WordPress install | ||
| Given a WP install | ||
|
|
||
| When I run `wp db status` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Database Name: | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Tables: | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Total Size: | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Prefix: wp_ | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Engine: | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Charset: | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Collation: | ||
| """ | ||
| And STDOUT should contain: | ||
| """ | ||
| Check Status: | ||
| """ | ||
|
|
||
| Scenario: Verify database status shows correct database name | ||
| Given a WP install | ||
|
|
||
| When I run `wp db status` | ||
| Then STDOUT should contain: | ||
| """ | ||
| wp_cli_test | ||
| """ | ||
|
|
||
| Scenario: Verify database status shows check status as OK | ||
| Given a WP install | ||
|
|
||
| When I run `wp db status` | ||
| Then STDOUT should contain: | ||
| """ | ||
| Check Status: OK | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Behat tests are split across multiple scenarios, each testing a small part of the command's output. This makes the tests less robust as they don't verify the output structure as a whole. Combining these into a single, more comprehensive scenario would improve test clarity and strength. Using should match with regular expressions for dynamic values like table count and size will also make the assertions more precise and resilient to minor changes.
Scenario: Display database status for a WordPress install
Given a WP install
When I run `wp db status`
Then STDOUT should contain:
"""
Database Name: wp_cli_test
"""
And STDOUT should match /^Tables:\s+\d+$/m
And STDOUT should match /^Total Size:\s+[\d.]+ \wB$/m
And STDOUT should contain:
"""
Prefix: wp_
"""
And STDOUT should match /^Engine:\s+\w+$/m
And STDOUT should match /^Charset:\s+\w+$/m
And STDOUT should match /^Collation:\s+\w+$/m
And STDOUT should contain:
"""
Check Status: OK
"""
| if ( empty( $db_size_bytes ) || $db_size_bytes <= 0 ) { | ||
| $db_size = '0 B'; | ||
| } else { | ||
| $size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) ); | ||
| $sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ]; | ||
| $size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0]; | ||
| $divisor = pow( 1000, $size_key ); | ||
| $db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block for formatting the database size into a human-readable string is very similar to logic within the size() method. To avoid code duplication and improve maintainability, consider extracting this logic into a private helper function that can be reused in both places. For now, the logic can be slightly simplified for better readability by inverting the conditional and using the null coalescing operator.
if ( ! empty( $db_size_bytes ) && $db_size_bytes > 0 ) {
$size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) );
$sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
$size_format = $sizes[ $size_key ] ?? $sizes[0];
$divisor = pow( 1000, $size_key );
$db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format;
} else {
$db_size = '0 B';
}| WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', $db_name ) ); | ||
| WP_CLI::log( sprintf( '%-18s %d', 'Tables:', $table_count ) ); | ||
| WP_CLI::log( sprintf( '%-18s %s', 'Total Size:', $db_size ) ); | ||
| WP_CLI::log( sprintf( '%-18s %s', 'Prefix:', $prefix ) ); | ||
| WP_CLI::log( sprintf( '%-18s %s', 'Engine:', $engine ) ); | ||
| WP_CLI::log( sprintf( '%-18s %s', 'Charset:', $charset ) ); | ||
| WP_CLI::log( sprintf( '%-18s %s', 'Collation:', $collation ) ); | ||
| WP_CLI::log( sprintf( '%-18s %s', 'Check Status:', $check_status ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The block of WP_CLI::log() calls for outputting the status is repetitive. This can be refactored to be data-driven by storing the status information in an associative array and then iterating over it to generate the output. This approach is more maintainable and makes it easier to add or modify status fields in the future.
$status_items = [
'Database Name' => $db_name,
'Tables' => $table_count,
'Total Size' => $db_size,
'Prefix' => $prefix,
'Engine' => $engine,
'Charset' => $charset,
'Collation' => $collation,
'Check Status' => $check_status,
];
foreach ( $status_items as $label => $value ) {
WP_CLI::log( sprintf( '%-18s %s', $label . ':', $value ) );
}Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Adds single command to retrieve database health and configuration info, consolidating what previously required running
wp db size,wp db tables,wp db prefix, andwp db checkseparately.Implementation
New
status()method inDB_Commandclassinformation_schema.TABLESfor size, engine, charset, collationmysqlcheckfor health status--dbuser,--dbpass, and--defaultsoptions for authenticationCommand registration in
composer.jsonBehat test coverage for output validation and credential options
Output
Notes
wp db size --human-readablebehavior@when before_wp_load), suitable for automation and use before WordPress is installed--dbuser,--dbpass,--defaults) for consistency with other db commandsOriginal prompt
wp db statuscommand for quick database health overview #306✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.